﻿using UnityEngine;
using System.IO;
using System.Collections;
using System.Collections.Generic;

public class TrackableList : MonoBehaviour {	
	private Object[] items;

	private GUIStyle style;
	private GUIStyle btnStyle;

	private bool ispopup = false;
	private Texture2D selectedTargetImage; 

	private float ratio;
	private TrackableInfo trackableInfo;

	// Use this for initialization
	void Start () 
    {
		trackableInfo = TrackableInfo.getInstance ();

		trackableInfo.PrefabPath = "Contents/";

		ratio = (float)Screen.width / Screen.height;

		style = new GUIStyle ();
		style.fontSize = 50;
		style.alignment = TextAnchor.MiddleCenter;

		btnStyle = new GUIStyle ();
		btnStyle.fontSize = 50;
		btnStyle.normal.textColor = Color.white;
		btnStyle.fontStyle = FontStyle.Bold;
		btnStyle.alignment = TextAnchor.MiddleCenter;

		items = Resources.LoadAll ("Contents", typeof(GameObject));
	}
	
	// Update is called once per frame
	void Update () 
    {
		if (Input.GetKeyDown(KeyCode.Escape)) 
        {
			Application.Quit();
		}
	}

	private Vector2 scrollPosition = Vector2.zero;
	private bool errorpopup = false;

	void OnGUI() 
    {
		if (!ispopup)
        {
			GUI.Label (new Rect ((Screen.width * 0.5f - Screen.width * 0.05f), Screen.height * 0.01f, Screen.width * 0.1f, Screen.height * 0.1f), "Trackable List", style);
			scrollPosition = GUI.BeginScrollView (new Rect (Screen.width * 0.03f, Screen.height * 0.2f, Screen.width * 0.94f, Screen.height * 0.7f), 
                     scrollPosition, new Rect (0, 0, Screen.width * 0.8f, Screen.height * 0.1f * items.Length)); // screen pos, current scroll posiiton, content area

			for (int i = 0; i < items.Length; i++) 
            {
				if (GUI.Button (new Rect (0, Screen.height * 0.1f * i, Screen.width * 0.94f, Screen.height * 0.1f), items [i].name)) 
                {
					GameObject go = Resources.Load (trackableInfo.PrefabPath + items [i].name) as GameObject;
					if (go != null) 
                    {
                        string trackablename = go.GetComponent<TrackableBehaviour>().TrackableNameList[go.GetComponent<TrackableBehaviour>().SelectedTrackableIndex];
						trackableInfo.Id = trackablename;
						Debug.Log("trackableInfo.Id : " + trackableInfo.Id);
						trackableInfo.Prefab = items[i].name;
						Debug.Log("trackableInfo.Prefab : " + trackableInfo.Prefab);

						selectedTargetImage = loadTargetImage(trackableInfo.Id);
						if (selectedTargetImage != null) 
                        {
							errorpopup = false;
							ispopup = true;
						} 
                        else 
                        {
							errorpopup = true;
						}
					}
				}
			}
			GUI.EndScrollView ();

            if (errorpopup)
            {
                GUI.Label(new Rect(Screen.width * 0.2f, Screen.height * 0.2f, Screen.width * 0.6f, Screen.height * 0.6f), "failed to load target image", btnStyle);
            }
		}

		if (ispopup) 
        {
			GUI.Box(new Rect(Screen.width * 0.25f, Screen.height * (0.5f - (0.5f * ratio * 0.5f)), Screen.width * 0.5f, Screen.height * 0.4f * ratio), "Target Image");
			GUI.DrawTexture(new Rect(Screen.width * 0.35f, Screen.height * (0.5f - (0.5f * ratio * 0.5f)) + Screen.height * 0.05f, Screen.width * 0.3f, Screen.height * 0.3f * ratio), selectedTargetImage);

			if (GUI.Button(new Rect(Screen.width * 0.35f, Screen.height * 0.65f, Screen.width * 0.1f, Screen.height* 0.08f), "Close")) 
            {
				ispopup = false;
			}

			if (GUI.Button(new Rect(Screen.width * 0.55f, Screen.height * 0.65f, Screen.width * 0.1f, Screen.height * 0.08f), "OK")) 
            {
				Application.LoadLevel("ARView");
			}
		}
	}

	WWW load;

	public Texture2D loadTargetImage(string imageName) 
    {
		Texture2D texture = (Texture2D)Resources.Load(imageName);

		return texture;
	}
}
